package com.dawnClass.service.impl;

import com.dawnClass.VO.VideoRateVO;
import com.dawnClass.mapper.ChapterMapper;
import com.dawnClass.mapper.LearningRateMapper;
import com.dawnClass.mapper.VideoMapper;
import com.dawnClass.pojo.Chapter;
import com.dawnClass.pojo.LearningRate;
import com.dawnClass.pojo.LearningRateExample;
import com.dawnClass.pojo.Video;
import com.dawnClass.service.LearningRateService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LearningRateServiceImpl implements LearningRateService {

    @Autowired
    private LearningRateMapper learningRateMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 保存用户学习节点
     * @return
     */
    @Override
    public ServiceResponse saveRate(LearningRate learningRate) {
        LearningRateExample learningRateExample = new LearningRateExample();
        learningRateExample.createCriteria()
                .andConsumerIdEqualTo(learningRate.getConsumerId())
                .andVideoIdEqualTo(learningRate.getVideoId())
                .andChapterIdEqualTo(learningRate.getChapterId());
        List<LearningRate> learningRates = learningRateMapper.selectByExample(learningRateExample);
        int row ;
        if (learningRates.size() > 0){
            System.out.println("存在学习记录,进行修改");
            //存在学习记录
            //获得该用户该视频上次学习记录
            LearningRate learningRateSQL = learningRates.get(0);

            //判断当前视频是否学完
            if (learningRateSQL.getEndLength() != 1){
                learningRateSQL.setEndTime(learningRate.getEndTime());
                if (learningRateSQL.getEndLength()<learningRate.getEndLength()){
                    learningRateSQL.setEndLength(learningRate.getEndLength());
                }
                if (learningRate.getEndLength() == 1)
                    learningRateSQL.setLearningState(1);
            }
            learningRateSQL.setCreateTime(learningRate.getCreateTime());
            //持久化到数据库
            System.out.println("即将更新的课程记录" + learningRateSQL);
            row = learningRateMapper.updateByPrimaryKey(learningRateSQL);
            if (row ==1 ){
                return ServiceResponse.updataSuccess("修改成功");
            }
            return ServiceResponse.updataFail("修改失败");

        }else{
            //不存在该课程的学习记录
            System.out.println("不存在学习记录,开始新建");
            learningRate.setLearningState(0);
            Chapter chapter = chapterMapper.selectByPrimaryKey(learningRate.getChapterId());
            learningRate.setCourseId(chapter.getCourseId());
            //创建一条新的学习记录持久化到数据库
            System.out.println("即将被新建的课程记录" + learningRate);
            row = learningRateMapper.insert(learningRate);
            if (row ==1 ){
                return ServiceResponse.saveSuccess("保存成功");
            }
            return ServiceResponse.saveFail("保存失败");
        }
    }

    /**
     * 获取当前用户学习节点
     * @param consumerId
     * @param videoId
     * @return
     */
    @Override
    public ServiceResponse getRate(Integer consumerId, Integer videoId) {
        LearningRateExample example = new LearningRateExample();
        example.createCriteria().andConsumerIdEqualTo(consumerId)
                .andVideoIdEqualTo(videoId);
        List<LearningRate> learningRates = learningRateMapper.selectByExample(example);
        if (learningRates.size()>0){
            //获得到节点，返回用户视频进度
            return ServiceResponse.getSuccess(learningRates.get(0).getEndTime());
        }
        //未得到节点，返回0;
        return ServiceResponse.getSuccess(0);
    }

    /**
     * 获取当前用户当前课程最后学习节点
     * @param consumerId
     * @param courseId
     * @return
     */
    @Override
    public ServiceResponse getCourseRate(Integer consumerId, Integer courseId) {
        LearningRateExample example = new LearningRateExample();
        example.createCriteria().andConsumerIdEqualTo(consumerId)
                .andCourseIdEqualTo(courseId);
        example.setOrderByClause("create_time desc  Limit 1");
        List<LearningRate> learningRates = learningRateMapper.selectByExample(example);
        if (learningRates.size()>0){
            Video video = videoMapper.selectByPrimaryKey(learningRates.get(0).getVideoId());
            return ServiceResponse.getSuccess(new VideoRateVO(video , learningRates.get(0)));
        }
        return ServiceResponse.getFail("null");
    }


}
